if(!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.permissions.Master")) {
    /**
     *
     */
    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.userMgt.permissions", function (machines) {
    });

    /**
     *
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.userMgt.permissions.Master",
            jsx3.gui.LayoutGrid,
            null,
            function (master) {
                master.prototype.initialize = function() {
                    var me = this
                    this.subscribeAll()
                    //this.callbuildUIResourceTree()
                    if(jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.window.Window")) {
                        // set the window instance
                        this.window = this.getAncestorOfType(tibco.admin.enterpriseAssets.userMgt.window.Window)
                        if(this.window) {
                            this.setWindowId(this.window.getId());
                        }
                    }
                    // set the matrix
                    this.mtx = this.getDescendantOfName("mtxMaster_24_k");
                    this.mtx.publishSelect = function(strRECORDIDS) {
                        me.publishSelect(strRECORDIDS);
                    };
                    setTimeout(function(){me.callbuildUIResourceTree()} ,200)

                }

                master.prototype.subscribeAll = function() {
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.SET_PERMISSION_SUCCESS, this, this.refreshDetail);
                }

                master.prototype.unsubscribeAll = function() {
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.userMgt.EventID.SET_PERMISSION_SUCCESS, this);
                }

                master.prototype.setWindowId = function(strId) {
                    this.windowId = strId
                }

                master.prototype.getWindowId = function() {
                    return this.windowId
                }

                master.prototype.setSelectedRecord = function(strRECORDID) {
                    this.selected = strRECORDID
                }

                master.prototype.getSelectedRecord = function() {
                    return this.selected
                }

                master.prototype.doRefresh = function() {
                    // this.callbuildUIResourceTree();
                }

                master.prototype.refreshDetail = function(objEvent) {
                    if(objEvent.windowId == this.getWindowId() && !objEvent.isRetained) {
                        this.callbuildUIResourceTree()
                    }

                }

                /**
                 * called when a user in user list is selected. publishes an event object (javascript object) with the
                 * following named properties: subject(tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED):
                 * windowId(id of the parent window instance); userId(userId); isWriteable(boolean) ;
                 * isGetGroupsFprUserEfficient(boolean)
                 *
                 *@param strRecordId {String} ?the jsxid attribute of the data record to return.(selected item)
                 */
                master.prototype.publishSelect = function(strRECORDIDS) {
                    var strRowID = strRECORDIDS[0]
                    this.setSelectedRecord(strRowID);
					var nameIdArr = new Array;
					var hasOwnerFlag = true;
					var firstId = "";
					var firstName = "";
                    if(strRECORDIDS.length > 0){
                        for(var i=0;i<strRECORDIDS.length;i++){
							var tmpRec = this.mtx.getXML().selectSingleNode("//record[@jsxid ='" + strRECORDIDS[i] + "']");
							if(i==0)
							{
								firstId = tmpRec.getAttribute("itemid");
								firstName = (dummy = tmpRec.getAttribute("display_name"))?dummy:"";
							}
							if(hasOwnerFlag){
								hasOwnerFlag = (tmpRec.getAttribute("hasOwner") == 'true')?true:false;
							}
							var obj = new Object();
                            obj.id = tmpRec.getAttribute("itemid");
                            obj.name = (dummy = tmpRec.getAttribute("display_name"))?dummy:"";
                            nameIdArr.push(obj);
						}
						var msg = new Object();
						msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED
						msg.windowId = this.getWindowId();
						msg.itemId = firstId;
						msg.display_name = firstName;
						msg.hasOwner = hasOwnerFlag;
						msg.nameIdArr = nameIdArr;
						msg.strPath = "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/Detail.xml"
						tibco.admin.PubSub.publish(msg);
					}
					else{
						var record = this.mtx.getXML().selectSingleNode("//record[@jsxid ='" + strRowID + "']");
						if(record) {
							var msg = new Object();
							msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED
							msg.windowId = this.getWindowId();
							msg.itemId = record.getAttribute("itemid");
							msg.display_name = (dummy = record.getAttribute("display_name"))?dummy:"";
							msg.path = (dummy = record.getAttribute("path"))?dummy:"";
							msg.hasOwner = (record.getAttribute("hasOwner") == 'true')?true:false;
							msg.hasEdit = (record.getAttribute("hasEdit") == 'true')?true:false;
							msg.strPath = "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/Detail.xml"
							tibco.admin.PubSub.publish(msg);
						}
					}
                }

                //call this method to begin the service call (eg.service.callbuildUIResourceTree();)
                master.prototype.callbuildUIResourceTree = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/rules/buildUIResourceTree.xml", "buildUIResourceTree", "Build UIResource Tree");
                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onbuildUIResourceTreeSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onbuildUIResourceTreeError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onbuildUIResourceTreeInvalid);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getPluginsEndpointURL());
                    objService.doCall();
                };

                master.prototype.onbuildUIResourceTreeSuccess = function(objEvent) {
                    this.mtx.setSourceXML(objEvent.target.getInboundDocument())
                    this.mtx.repaint()
                    if(this.getSelectedRecord()) {
                        // select the detalis for this selected
                        var array = new Array;
                        array[0] = this.getSelectedRecord();
                        this.publishSelect(array);
                        this.mtx.selectRecord(this.getSelectedRecord());
                    }
                };

                master.prototype.onbuildUIResourceTreeError = function(objEvent) {
                    var myStatus = objEvent.target.getRequest().getStatus();
                    tibco.admin.util.Log.USERMGT.error("The service call failed. The HTTP Status code is: " + myStatus);
                };

                master.prototype.onbuildUIResourceTreeInvalid = function(objEvent) {
                    tibco.admin.util.Log.USERMGT.error("The following message node just failed validation:\n\n" + objEvent.message);
                };

            });
}
